/*
 * $Id: TeamSelectFrame.java 14 2008-12-04 13:13:33Z rdubisz $
 */
package wdp.worker;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import wdp.WdpApp;
import wdp.entities.ready.Team;

public class TeamSelectFrame extends JDialog {

    public TeamSelectFrame(JFrame parent) {
        super(parent, true);
        initComponents();
        TableSelectionListener listener = new TableSelectionListener();
        masterTable.getSelectionModel().addListSelectionListener(listener);
        detailTable.getSelectionModel().addListSelectionListener(listener);
        entityManager.getTransaction().begin();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

    entityManager = javax.persistence.Persistence.createEntityManagerFactory("db.fdbPU").createEntityManager();
    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(TeamSelectFrame.class);
    query = entityManager.createQuery(resourceMap.getString("query.query")); // NOI18N
    list = org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
    masterScrollPane = new javax.swing.JScrollPane();
    masterTable = new javax.swing.JTable();
    newButton = new javax.swing.JButton();
    detailScrollPane = new javax.swing.JScrollPane();
    detailTable = new javax.swing.JTable();
    jButtonSelect = new javax.swing.JButton();
    jButtonNone = new javax.swing.JButton();

    FormListener formListener = new FormListener();

    setTitle(resourceMap.getString("Select team.title")); // NOI18N
    setBounds(new java.awt.Rectangle(100, 100, 400, 300));
    setModal(true);
    setName("Select team"); // NOI18N

    masterScrollPane.setName("masterScrollPane"); // NOI18N

    masterTable.setName("masterTable"); // NOI18N

    org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
    org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
    columnBinding.setColumnName("Name");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    bindingGroup.addBinding(jTableBinding);
    jTableBinding.bind();
    masterTable.addMouseListener(formListener);
    masterScrollPane.setViewportView(masterTable);

    newButton.setText(resourceMap.getString("newButton.text")); // NOI18N
    newButton.setName("newButton"); // NOI18N
    newButton.addActionListener(formListener);

    detailScrollPane.setName("detailScrollPane"); // NOI18N

    detailTable.setName("detailTable"); // NOI18N

    org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${selectedElement.workerCollection}");
    jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, eLProperty, detailTable);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${firstname}"));
    columnBinding.setColumnName("Firstname");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${surname}"));
    columnBinding.setColumnName("Surname");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idPost}"));
    columnBinding.setColumnName("Id Post");
    columnBinding.setColumnClass(wdp.entities.ready.Post.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${office}"));
    columnBinding.setColumnName("Office");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${loginName}"));
    columnBinding.setColumnName("Login Name");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${hired}"));
    columnBinding.setColumnName("Hired");
    columnBinding.setColumnClass(java.util.Date.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fired}"));
    columnBinding.setColumnName("Fired");
    columnBinding.setColumnClass(java.util.Date.class);
    columnBinding.setEditable(false);
    bindingGroup.addBinding(jTableBinding);
    jTableBinding.bind();
    detailScrollPane.setViewportView(detailTable);

    jButtonSelect.setText(resourceMap.getString("jButtonSelect.text")); // NOI18N
    jButtonSelect.setName("jButtonSelect"); // NOI18N
    jButtonSelect.addActionListener(formListener);

    jButtonNone.setText(resourceMap.getString("jButtonNone.text")); // NOI18N
    jButtonNone.setName("jButtonNone"); // NOI18N
    jButtonNone.addActionListener(formListener);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(newButton)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButtonNone)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButtonSelect))
          .addComponent(detailScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jButtonSelect)
          .addComponent(newButton)
          .addComponent(jButtonNone))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(detailScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );

    bindingGroup.bind();
  }

  // Code for dispatching events from components to event handlers.

  private class FormListener implements java.awt.event.ActionListener, java.awt.event.MouseListener {
    FormListener() {}
    public void actionPerformed(java.awt.event.ActionEvent evt) {
      if (evt.getSource() == newButton) {
        TeamSelectFrame.this.newButtonActionPerformed(evt);
      }
      else if (evt.getSource() == jButtonSelect) {
        TeamSelectFrame.this.jButtonSelectActionPerformed(evt);
      }
      else if (evt.getSource() == jButtonNone) {
        TeamSelectFrame.this.jButtonNoneActionPerformed(evt);
      }
    }

    public void mouseClicked(java.awt.event.MouseEvent evt) {
      if (evt.getSource() == masterTable) {
        TeamSelectFrame.this.masterTableMouseClicked(evt);
      }
    }

    public void mouseEntered(java.awt.event.MouseEvent evt) {
    }

    public void mouseExited(java.awt.event.MouseEvent evt) {
    }

    public void mousePressed(java.awt.event.MouseEvent evt) {
    }

    public void mouseReleased(java.awt.event.MouseEvent evt) {
    }
  }// </editor-fold>//GEN-END:initComponents

    public Team getSelectedEntity() {
        return selectedEntity;
    }

    /**
     * TODO dodać zaznaczanie na liście wybranej encji
     */
    public void setSelectedEntity(Team selectedEntity) {
        this.selectedEntity = selectedEntity;
    //if(selectedEntity == null)
    //masterTable.setSetSelectedRow(-1);
    //else
    }

    private class TableSelectionListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (e.getSource() == masterTable.getSelectionModel()) {
                if (masterTable.getSelectedRow() != -1) {
                    selectedEntity = list.get(masterTable.convertRowIndexToModel(masterTable.getSelectedRow()));
                } else {
                    setSelectedEntity(null);
                }
            } else {
                setSelectedEntity(null);
            }
        }
    }

    public static Team selectEntity() {
        TeamSelectFrame win = new TeamSelectFrame(null);
        win.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(win);
        return win.getSelectedEntity();
    }

  private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        wdp.entities.ready.Team t = new wdp.entities.ready.Team();
      entityManager.persist(t);
      list.add(t);
      int row = list.size() - 1;
      masterTable.setRowSelectionInterval(row, row);
      masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
  }//GEN-LAST:event_newButtonActionPerformed

  private void jButtonSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectActionPerformed
      setVisible(false);
  }//GEN-LAST:event_jButtonSelectActionPerformed

  private void masterTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_masterTableMouseClicked
      if (evt.getClickCount() > 1) {
          setVisible(false);
      }
  }//GEN-LAST:event_masterTableMouseClicked

  private void jButtonNoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNoneActionPerformed
      selectedEntity = null;
      setVisible(false);
  }//GEN-LAST:event_jButtonNoneActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JScrollPane detailScrollPane;
  private javax.swing.JTable detailTable;
  private javax.persistence.EntityManager entityManager;
  private javax.swing.JButton jButtonNone;
  private javax.swing.JButton jButtonSelect;
  private java.util.List<wdp.entities.ready.Team> list;
  private javax.swing.JScrollPane masterScrollPane;
  private javax.swing.JTable masterTable;
  private javax.swing.JButton newButton;
  private javax.persistence.Query query;
  private org.jdesktop.beansbinding.BindingGroup bindingGroup;
  // End of variables declaration//GEN-END:variables
    private Team selectedEntity = null;
}
